import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:school_safe_teacher/resourse/app_colors.dart';
import 'package:school_safe_teacher/widget/my_widget.dart';
import 'package:school_safe_teacher/widget/no_message_widget.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    attendanceRecordState state, Dispatch dispatch, ViewService viewService) {
  return body(
      child: Container(
        color: AppColors.MainBackColor,
        child: Column(children: [
          Container(
            child: myBar(context: viewService.context, title: "考勤列表"),
            color: Colors.white,
          ),
          Expanded(
              child: EasyRefresh(
                  controller: state.easyRefreshController,
                  header: MaterialHeader(
                      completeDuration: Duration(milliseconds: 0)),
                  footer: MaterialFooter(),
                  firstRefresh: true,
                  enableControlFinishRefresh: true,
                  enableControlFinishLoad: true,
                  onRefresh: () async {
                    //下拉刷新
                    state..page = 1;
                    dispatch(attendanceRecordActionCreator.onRefresh());
                  },
                  onLoad: () async {
                    //上拉加载
                    state..page = state.page + 1;
                    dispatch(attendanceRecordActionCreator.onload());
                  },
                  child: _list(state, dispatch, viewService)))
        ]),
      ),
      context: viewService.context);
}

Widget _list(
    attendanceRecordState state, Dispatch dispatch, ViewService viewService) {
  if (state.list.isNotEmpty) {
    return ListView.builder(
      padding: EdgeInsets.zero,
      itemBuilder: (BuildContext contentext, int index) {
        return Container(
          padding: EdgeInsets.only(top: 10, bottom: 10, left: 15, right: 15),
          margin: EdgeInsets.only(top: 5, bottom: 5, left: 15, right: 15),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.elliptical(10, 10)),
            color: Colors.white,
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Row(children: [
                Icon(
                  Icons.calendar_today_sharp,
                  color: Colors.yellow,
                  size: 15,
                ),
                Container(
                  margin: EdgeInsets.only(left: 10),
                  child: Text(
                    state.list[index].createTime,
                    style: TextStyle(
                        color: AppColors.deepTextColor,
                        fontSize: 15,
                        fontWeight: FontWeight.bold),
                  ),
                )
              ]),
              Divider(),
              Container(
                child: Row(
                  children: [
                    Image.asset(
                      "images/student_choice@2x.png",
                      width: 5,
                      height: 5,
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 5),
                      child: Text(
                        "应到：" + state.list[index].shouldNum.toString() + "人  ",
                        style: TextStyle(
                            color: AppColors.middleTextColor, fontSize: 14),
                      ),
                    )
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 15),
                child: Row(
                  children: [
                    Image.asset(
                      "images/student_choice@2x.png",
                      width: 5,
                      height: 5,
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 5),
                      child: Text(
                        "实到: " + state.list[index].realityNum.toString() + "人",
                        style: TextStyle(
                            color: AppColors.middleTextColor, fontSize: 14),
                      ),
                    )
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 15),
                child: Row(
                  children: [
                    Image.asset(
                      "images/student_choice@2x.png",
                      width: 5,
                      height: 5,
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 5),
                      child: Text(
                        "缺勤: " +
                            state.list[index].lackNum.toString() +
                            "人 ( " +
                            state.list[index].lackName +
                            " ）",
                        style: TextStyle(
                            color: AppColors.middleTextColor, fontSize: 14),
                      ),
                    )
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 15),
                child: Row(
                  children: [
                    Image.asset(
                      "images/student_choice@2x.png",
                      width: 5,
                      height: 5,
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 5),
                      child: Text(
                        state.list[index].truancyNum > 0
                            ? "旷课: " +
                                state.list[index].truancyNum.toString() +
                                "人 ( " +
                                state.list[index].truancyName +
                                " ）"
                            : "旷课: " +
                                state.list[index].truancyNum.toString() +
                                "人",
                        style: TextStyle(
                            color: AppColors.middleTextColor, fontSize: 14),
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
        );
      },
      itemCount: state.list.length,
    );
  } else {
    return noMemberWidget();
  }
}
